var util = require('../../../utils/util.js');
const db = wx.cloud.database();
//获取应用实例
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hideLoading: false,
    resourceList: []
  },

  getIndexData: function () {
    let that = this;
    var openid = app.globalData.openid;
    db.collection('home_resource').where({
      _openid: openid
    }).get().then(res => {
      that.setData({resourceList: res.data });
    });

    setTimeout(function () {
      that.setData({
        hideLoading: true
      });
    }, 300);
  },

  //跳转到详情页 
  gotoArticle: function (e) {
    var idx = e.currentTarget.dataset.index;
    var item = this.data.resourceList[idx];
    let data = JSON.stringify(item);
    wx.navigateTo({
      url: '/pages/article/article?data=' + data
    })
  },

  //发布资源
  publishResource: function (e) {
    //如果已经登陆,且还没有记录
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        userInfo: e.detail.userInfo
      });
      app.globalData.userInfo = e.detail.userInfo;
    }

    if (null == this.data.userInfo) {
      util.showErrorToast("匿名不能发表");
      return;
    }

    wx.navigateTo({
      url: '/pages/ucenter/publish/publish?type=resource'
    });
  },

  // 点击删除按钮
  handleDelete: function (e) {
    var index = e.currentTarget.dataset.idx;
    var id = e.currentTarget.dataset.id;
    //验证有无评论
    db.collection('common_comment').where({
      topic_id: id
    }).count().then(res => {
      const count = res.total;
      if (count > 0) {
        util.showErrorToast("已被评论");
      } else {
        db.collection('home_resource').doc(id).remove().then(res => {
          //被删除的资源
          var delResource = this.data.resourceList[index];
          this.data.resourceList.splice(index, 1);
          this.setData({ resourceList: this.data.resourceList });
          wx.showToast({ title: "删除成功" });

          //资源删除了，它下面的图片，也连带删除
          var imgs = [].concat(delResource.imgs).concat(delResource.headImg);
          if (null != imgs && imgs.length > 0) {
            wx.cloud.deleteFile({
              fileList: imgs,
              success: res => {
              },
              fail: res => {
              }
            })
          }

        });
      }
    })
  },
  // 左滑显示删除按钮
  handleSliderLeftStart: function (e) {
    this.data.resourceList.forEach(todoItem => {
      // 除了当前项，其它打开项的菜单都关闭，确保每次只有一个项可以左滑显示删除
      if (todoItem.id !== e.target.dataset.id && todoItem.isOpen) {
        todoItem.isOpen = false
      }
    });
    this.setData({
      resourceList: this.data.resourceList
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIndexData();
  },

  onPullDownRefresh:function()
  {
    this.getIndexData();
    this.setData({
      hideLoading: false
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})